package com.salinity.kun.task;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.sql.Connection;
import java.sql.SQLException;
import java.text.ParseException;

import com.salinity.kun.helper.OracleHelper;

public class UpdateTideTask implements ITask {

	private Path inputPath;
	private String tableName;
	
	 public UpdateTideTask(Path inputPath,String tableName) {
		 this.inputPath = inputPath;
		 this.tableName = tableName;
	}
	public static void main(String[] args) {
		String fluxTable = "t_szhd_swdtslxx";
		// Path DTLLinputPath =
		// Paths.get("E:\\Kuner\\SongLaoShi\\表数据\\2018-05-10\\大通数据.sql");
		// String DTLLTable = "t_szhd_rgswllxx";
		//
		// Path NTLLinputPath =
		// Paths.get("E:\\Kuner\\SongLaoShi\\表数据\\2018-05-10\\南通数据.sql");
		// String NTTable = "t_szhd_swdtslxx";

		// Path LHK_flux = Paths.get("E:\\Kuner\\研究生\\论文相关\\data\\浏河口\\处理后.sql");
		// String fluxTable = "t_szhd_swdtslxx";

		// Path LHK_flux = Paths.get("E:\\Kuner\\SongLaoShi\\表数据\\镇江2(浮子式).sql");

		// Path LHK_flux =
		// Paths.get("E:\\Kuner\\研究生\\论文相关\\data\\江阴，白茆，南京水位数据\\白茆.sql");

		// Path LHK_flux =
		// Paths.get("E:\\Kuner\\研究生\\论文相关\\data\\江阴，白茆，南京水位数据\\江阴.sql");

		Path LHK_flux = Paths.get("E:\\Kuner\\研究生\\论文相关\\data\\江阴，白茆，南京水位数据\\南京.sql");
		ITask task = new UpdateTideTask(LHK_flux, fluxTable);
		try {
			task.run();
		} catch (IOException | ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		

	}

	
	@Override
	public boolean run() throws IOException, ParseException {



		// Path LHK_flux = Paths.get("");
		OracleHelper oracleHelper = new OracleHelper();
		Connection con = oracleHelper.prepareConection();
		try {
			
			updateData(inputPath, con, tableName);
			System.out.println("流量更新完成...");
			// updateData(NTLLinputPath,con,NTTable);
			// System.out.println("南通实测数据更新完成...");
		} catch (SQLException | IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			try {
				con.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return true;
	}
	public boolean updateData(Path inputPath, Connection con, String tableName) throws SQLException, UnsupportedEncodingException, FileNotFoundException, IOException {
		try (BufferedReader br = new BufferedReader(
				new InputStreamReader(new FileInputStream(inputPath.toFile()), "gbk"))) {

			String line = null;
			StringBuilder sqlBuilder = new StringBuilder();
			System.out.println("正在插入数据到表： " + tableName + "中，请稍等...");
			int count = 0;
			while ((line = br.readLine()) != null) {
				if (line.equals("")) {
					OracleHelper.excuteSql(con, sqlBuilder.toString().replace(";", ""));
					count++;
					sqlBuilder.delete(0, sqlBuilder.length());
				} else {
					sqlBuilder.append(line + " ");
				}
			}
			System.out.println("数据插入完成，共插入：" + count + " 条数据...");
			System.out.println("正在删除表 " + tableName + "中的重复记录，请稍等...");
			OracleHelper.deduplication(con, tableName);
			System.out.println("删除表 " + tableName + "中的重复记录完成...");

		}
		return true;
	}

}
